package com.avic.rong.questionOrAnswer.adapter.video;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import com.avic.rong.R;
import com.avic.rong.adapter.vadio_adapter.RecyclerItemBaseHolder;
import com.shuyu.gsyvideoplayer.GSYVideoManager;
import com.shuyu.gsyvideoplayer.builder.GSYVideoOptionBuilder;
import com.shuyu.gsyvideoplayer.listener.GSYSampleCallBack;
import com.shuyu.gsyvideoplayer.video.GSYADVideoPlayer;
import com.shuyu.gsyvideoplayer.video.StandardGSYVideoPlayer;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

import static com.avic.rong.utils.permission.PermissionManager.TAG;

public class VideoNormalHolder extends RecyclerItemBaseHolder {

    private final Context context;
    StandardGSYVideoPlayer gsyVideoPlayer;
    ImageView imageView;
    GSYVideoOptionBuilder gsyVideoOptionBuilder;
    @BindView(R.id.image_header)
    ImageView image_header;
    @BindView(R.id.tv_date)
    TextView tv_date;
    @BindView(R.id.tv_position)
    TextView tv_position;
    @BindView(R.id.tv_name)
    TextView tv_name;
    @BindView(R.id.tv_collect)
    TextView tv_collect;
    @BindView(R.id.tv_zan)
    TextView tv_zan;
    @BindView(R.id.tv_comment)
    TextView tv_comment;
    @BindView(R.id.cb_collect)
    ImageView cb_collect;
    @BindView(R.id.cb_zan)
    ImageView cb_zan;
    @BindView(R.id.cb_comment)
    CheckBox cb_comment;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_length)
    TextView tvLength;
    public VideoNormalHolder(Context context, View v) {
        super(v);

        this.context = context;

        ButterKnife.bind(this,v);

        gsyVideoPlayer = v.findViewById(R.id.video_item_player);

        imageView = new ImageView(context);

        gsyVideoOptionBuilder = new GSYVideoOptionBuilder();


    }

    public void onBind(final int position,String url) {


        //增加封面

        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);

        imageView.setBackgroundResource(R.mipmap.asd_stan);

        if (imageView.getParent() != null) {

            ViewGroup viewGroup = (ViewGroup) imageView.getParent();

            viewGroup.removeView(imageView);

        }





        Map<String, String> header = new HashMap<>();

        header.put("ee", "33");


        //防止错位，离开释放

        gsyVideoPlayer.initUIState();

        gsyVideoOptionBuilder

                .setIsTouchWiget(false)

                .setThumbImageView(imageView)

                .setUrl(url)

                .setVideoTitle("")

                .setCacheWithPlay(false)

                .setRotateViewAuto(true)

                .setLockLand(true)

                .setPlayTag(TAG)

                .setMapHeadData(header)

                .setShowFullAnimation(true)

                .setNeedLockFull(true)

                .setPlayPosition(position)

                .setVideoAllCallBack(new GSYSampleCallBack() {

                    @Override

                    public void onPrepared(String url, Object... objects) {

                        super.onPrepared(url, objects);

                        if (!gsyVideoPlayer.isIfCurrentIsFullscreen()) {

                            //静音

                            GSYVideoManager.instance().setNeedMute(true);

                        }


                    }


                    @Override

                    public void onQuitFullscreen(String url, Object... objects) {

                        super.onQuitFullscreen(url, objects);

                        //全屏不静音

                        GSYVideoManager.instance().setNeedMute(true);

                    }


                    @Override

                    public void onEnterFullscreen(String url, Object... objects) {

                        super.onEnterFullscreen(url, objects);

                        GSYVideoManager.instance().setNeedMute(false);

                        gsyVideoPlayer.getCurrentPlayer().getTitleTextView().setText((String) objects[0]);

                    }

                }).build(gsyVideoPlayer);


        //增加title

        gsyVideoPlayer.getTitleTextView().setVisibility(View.GONE);


        //设置返回键

        gsyVideoPlayer.getBackButton().setVisibility(View.GONE);

        //设置播放键
       /* View startButton = gsyVideoPlayer.getStartButton();
        startButton.setBackgroundResource(R.mipmap.class_mulu_start);*/



        //设置全屏按键功能
        gsyVideoPlayer.getFullscreenButton().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                resolveFullBtn(gsyVideoPlayer);
            }
        });

    }


    /**
     * 全屏幕按键处理
     */

    private void resolveFullBtn(final StandardGSYVideoPlayer standardGSYVideoPlayer) {

        standardGSYVideoPlayer.startWindowFullscreen(context, true, true);

    }

}
